﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XF.Common.Redis;
using XF.Common.WeChat;
using XF.Meeting.Common;

namespace QuartzJob
{
    public class GetUserInfobyWechat : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            try
            {
                var a = RedisCache.Get<string>("depMinID", 1);
               // RedisCache.Set("heihei", "我进来了", 1);
                var wx = new WeChatHelper();
                //RedisCache.Set("token", wx, 1);
                if (!int.TryParse(a, out int min))
                {
                    var deplist = wx.GetDepartments().Result;

                    if (deplist.errcode == 0)
                    {
                        var list = deplist.department;
                        min = list.Min(i => i.id);
                        RedisCache.Set("depMinID", min, 1);
                    }
                }
                var user = wx.GetUserList(min, 1).Result;
                if (user.errcode == 0)
                {
                    foreach (var item in user.userlist)
                    {
                        RedisCache.Hash_Set("UserPhone", item.mobile, item.userid, 1);
                    }
                }
            }
            catch (SchedulerException e)
            {
             
                throw e;
            
            }

            return Task.CompletedTask;
        }
    }
}
